<?php
$relPath="./../../pinc/";
require_once $relPath."dpinit.php";
include_once($relPath.'dp_main.inc');

$projectid          = ArgProjectId();
// $username           = Arg("username");
$pagename           = Arg("pagename");
if($pagename != "")
    $pagenames = array($pagename);
else {
    $pagenames          = ArgArray('pagenames');
    $pagenames          = array_keys($pagenames);
}

$User->IsLoggedIn()
    or redirect_to_home();

($projectid != "" && count($pagenames) > 0)
    or die("Insufficient arguments provided"); 

$where = "WHERE pe.projectid = '$projectid'\n";
if(count($pagenames) == 1) {
    $where .= "AND pe.page_name = '{$pagenames[0]}'";
}
else {
    $where .= "AND pe.page_name IN 
            (".implode(', ', $pagenames).")";
}

$sql = "
    SELECT
        p.nameofwork AS title,
        p.authorsname AS author,
        FROM_UNIXTIME(pe.event_time) AS etime,
        pe.event_type	 AS etype,
        pe.projectid,
        pe.page_name,
        pe.task_code,
        pe.username
    FROM page_events pe
    JOIN projects p
        ON pe.projectid = p.projectid
    $where
    ORDER BY
        pe.page_name,
        pe.event_time";

$rows = $dpdb->SqlRows($sql);

if(count($rows) == 0) {
    die("No events.");
}

$title = $rows[0]['title'];
$author = $rows[0]['author'];

$tbl = new DpTable();

$tbl->AddColumn("^"._("Page"),  "page_name");
$tbl->AddColumn("^"._("Time"),  "etime");
$tbl->AddColumn("^"._("Task"),  "task_code");
$tbl->AddColumn("^"._("Event"), "etype");
$tbl->AddColumn("^"._("User"),  "username");

$tbl->SetRows($rows);



dp_header(_("Page log"));

echo "<h3>"._("Page log for")."</h3>
      <h2>$title</h2>
      <h3>$author</h3>\n";


$tbl->EchoTable();

dp_footer();

?>
